var db = require('../../db/db');
var publictool = require("../../publictool/publictool");
var moment = require('moment');

exports.port = function (req, res) {
    var requestData = '';
    req.addListener('data', function (postDataChunk) {
        requestData += postDataChunk;
    });

    req.addListener('end', function () {
        publictool.putlog(req, requestData);
        var phone = publictool.checkoutdata(res, requestData);
        if (!phone) {
            return;
        }

        var client = db.client();
        db.checkuser(client, res, false, phone.accesstoken)
            .then(function (resultvalue) {
                getresult(phone, res, client,resultvalue.account);
            });
    });
};

function getresult(phone, res, client,account) {
    var sql = 'SELECT * FROM receipt where bundleid=? AND productid=? AND account=?';
    var addSqlParams = [phone.bundleId, phone.product_id,account];
    var status = new Number(phone.status);
    if (status != 2) {
        sql = 'SELECT * FROM receipt where bundleid=? AND productid=? AND status=? AND account=?';
        addSqlParams = [phone.bundleId, phone.product_id, phone.status,account];
    }

    if(phone.idfv){
        sql += ' AND idfv=?';
        addSqlParams.push(phone.idfv);
    }

    client.query(sql, addSqlParams, function (err, result) {
        if (err) {
            publictool.returnErr(res, '查询参数错误:' + '[SELECT ERROR] - ' + err.message, 1);
            client.end();
            return;
        }

        var value = processresult(result,phone.num,phone.start_earch_timestamp,phone.end_earch_timestamp);
        publictool.returnValue(res, value, '成功');
        client.end();
        return;
    });
}

function processresult(result,num,start_earch_timestamp,end_earch_timestamp) {
    var value = {
        data: result
    };

    if (!result.length) {
        return value;
    }

    var bundlearr = [];
    for(var i = 0; i < result.length; ++i){
        var resultvalue = result[i];
        if(bundlearr.indexOf(resultvalue.bundleid) == -1){
            bundlearr.push(resultvalue.bundleid);
        }
    }

    var resultarr = [];
    for(var i = 0; i < bundlearr.length;++i){
        var key = bundlearr[i];
        var resultv = {};
        resultv[key] = [];
        resultarr.push(resultv);
    }

    for(var i = 0; i < result.length;++i){
        var resultvalue = result[i];
        var key = resultvalue.bundleid;
        var index = bundlearr.indexOf(key);
        if (index == -1){
            break;
        }
        var addvalue = {
            "prodcunt_id":resultvalue.productid,
            "price":resultvalue.price,
            "desc":resultvalue.des,
            "sn":resultvalue.sn,
            "receipt":resultvalue.receipt,
            "timestamp":resultvalue.timestamp,
            "status":resultvalue.status,
            "uuid":resultvalue.uuid,
            "idfv":resultvalue.idfv,
            "msg":resultvalue.msg,
            "product_index":resultvalue.product_index
        }
        var data = resultarr[index];
        if(!data){
            break;
        }
        var key = resultvalue.bundleid;
        var arr = data[key];
        arr.push(addvalue);
    }

    if(start_earch_timestamp){
        if(end_earch_timestamp){
            var start = moment(start_earch_timestamp).toDate().getTime();
            var end = moment(end_earch_timestamp).toDate().getTime();
            for(var i = 0; i < resultarr.length;++i){
                var data = resultarr[i];
                var key = bundlearr[i];
                var arr = data[key];
                var arrnew = [];
                for(var i = 0 ; i < arr.length; ++i){
                    var value = arr[i];
                    var time = moment(value.timestamp).toDate().getTime();
                    if(time >= start && time <= end){
                        arrnew.push(value);
                    }
                }
                data[key] = arrnew;
            }
        }else{
            var start = moment(start_earch_timestamp).toDate().getTime();
            for(var i = 0; i < resultarr.length;++i){
                var data = resultarr[i];
                var key = bundlearr[i];
                var arr = data[key];
                var arrnew = [];
                for(var i = 0 ; i < arr.length; ++i){
                    var value = arr[i];
                    var time = moment(value.timestamp).toDate().getTime();
                    if(time >= start){
                        arrnew.push(value);
                    }
                }
                data[key] = arrnew;
            }
        }
    }else if(end_earch_timestamp){
            var end = moment(end_earch_timestamp).toDate().getTime();
            for(var i = 0; i < resultarr.length;++i){
                var data = resultarr[i];
                var key = bundlearr[i];
                var arr = data[key];
                var arrnew = [];
                for(var i = 0 ; i < arr.length; ++i){
                    var value = arr[i];
                    var time = moment(value.timestamp).toDate().getTime();
                    if(time <= end){
                        arrnew.push(value);
                    }
                }
                data[key] = arrnew;
            }
    }

    var number = new Number(num);
    if(number > 0){
        for(var i = 0; i < resultarr.length;++i){
            var data = resultarr[i];
            var key = bundlearr[i];
            var arr = data[key];
            if(number < arr.length){
                var arrnew = [];
                for(var i = arr.length - num;i < arr.length;++i){
                    arrnew.push(arr[i]);
                }
                data[key] = arrnew;
            }
        }
    }

    value.data = resultarr;

    return value;
}